package com.frame.work.frame.deal.device0faa;

import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.constant.RabbitTopic;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.common.util.Util;
import com.frame.work.config.SpringUtil;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.mapper.TblDeviceActionsMapper;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import java.util.HashMap;
import java.util.Map;

@Data
public class ShuangSeWen_0FAA0D02 extends BaseFrame {
    private String switchState;//开关状态
    private String spreadingType;

    public ShuangSeWen_0FAA0D02(String gatewayMac, String data) {
        super(gatewayMac, data);
        RabbitTemplate rabbitTemplate = SpringUtil.getBean(RabbitTemplate.class);
        TblDeviceActionsMapper deviceActionsMapper = SpringUtil.getBean(TblDeviceActionsMapper.class);

        String mode;//灯状态
        String duration;//持续时间
        String delayTime;//延时时间
        String setColorTemperature;//设置色温
        String setBrightness;//设置亮度
        //将基础数据进行处理
        this.setSpreadingType(StringUtils.substring(data, 38, 40));

        //处理此设备特有的数据
        mode = StringUtils.substring(data, 58, 60);

        delayTime = StringUtils.substring(data, data.length() - 20, data.length() - 16);
        duration = StringUtils.substring(data, data.length() - 24, data.length() - 20);

        setBrightness = StringUtils.substring(data, data.length() - 8, data.length() - 4);
        setColorTemperature = StringUtils.substring(data, data.length() - 4, data.length());


        if (!"02".equals(mode)) {
            switchState = "01";
        } else {
            switchState = "02";
        }

        if ("82".equals(mode) || "8D".equals(mode)) {
            switchState = deviceActionsMapper.getSwitch(this.getDeviceMac());
        }

        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        //高地位变换
        actions.put("switchState", switchState);

        duration = Integer.parseInt(Util.twoReplace(duration), 16) + "";
        delayTime = Integer.parseInt(Util.twoReplace(delayTime), 16) + "";
        setBrightness = (((float) Integer.parseInt(Util.twoReplace(setBrightness), 16) / 30)) + "";
        setColorTemperature = ((Integer.parseInt(Util.twoReplace(setColorTemperature), 16) / 30)) + "";
//        nowBrightness = (Integer.parseInt(Util.twoReplace(nowBrightness), 16) / 30) + "";
//        nowColorTemperature = (Integer.parseInt(Util.twoReplace(nowColorTemperature), 16) / 30) + "";
        if (StringUtils.equals("0.0", setBrightness) && !mode.equals("02") && !mode.equals("82")) {
            setBrightness = "1";
        } else if (StringUtils.equals("0.6666667", setBrightness)) {
            setBrightness = "2";
        } else if (StringUtils.equals("2.0", setBrightness)) {
            setBrightness = "3";
        }
        actions.put("duration", duration);
        actions.put("delayTime", delayTime);
        actions.put("mode", mode);
        actions.put("setBrightness", (((int) (Float.parseFloat(setBrightness)))) + "");
        actions.put("setColorTemperature", setColorTemperature);
//        actions.put("nowBrightness", Util.twoReplace(nowBrightness));
//        actions.put("nowColorTemperature", Util.twoReplace(nowColorTemperature));
        this.setActions(actions);
        String json = JSONObject.toJSONString(this);
        //将数据发送到mqtt队列中


        if ("80".equals(this.getDataType()) && StringUtils.startsWith(mode, "0")) {
            JsonToMqtt.sendMQTT(gatewayMac, json);
        } else if ("8D".equals(mode)) {
            rabbitTemplate.convertAndSend(RabbitTopic.deviceDelayTime, this.getDeviceMac());
            this.setActions(null);
            JsonToMqtt.sendMQTT(gatewayMac, json);
        } else {
            if ("00".equals(this.getDataType())) {
                this.setActions(null);
                JsonToMqtt.sendMQTT(gatewayMac, json);
            }
            if ("82".equals(mode) && !"0".equals(delayTime)) {
                JsonToMqtt.sendBaseMQTT(gatewayMac, json);
            }
        }
    }


    public static void main(String[] args) {
        System.out.println(((int) (float) (60 / 30)) + "");
    }
}
